<?php
class MenusController extends AppController {
	var $name         = 'Menus';
    var $pageTitle    = 'Menu Sidebar Management';
    var $prefixSelect = '';

	function view($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Menu', true), array('action'=>'index'));
		}
		$this->set('menu', $this->Menu->read(null, $id));
	}
    
    
    function delete($id = null) {
		if (!$id) {
			$this->flash(__('Invalid Menu', true), array('action'=>'index'));
		}
        
		if ($this->Menu->del($id)) {
			$this->flash(__('Menu deleted', true), array('action'=>'index'));
		} else {
            // this is when delete operation failed
            $this->flash(__('Abort deleted', true), array('action'=>'index'));
		}
	}
    
    /**
     * Mendapatkan semua menu
     * Ini diakses melalui method requestAction oleh
     * element menu.call_name.ctp
     * 
     * @author gedex
     * @access public
     * @return mixed mengembalikan array berupa menu jika diakses oleh element
     *               jika diakses melalui URL maka akan mengembalikan false
     */
    function menu($type = null) {
        // hindari direquest melalui URL
        if ( isset($this->params['requested']) ) {
            $types = $this->Menu->MenuType->find('list', array('fields' => array('MenuType.call_name', 'MenuType.id')));
            $menuName = $this->Menu->MenuType->find('first', 
                array(
                    'conditions' => array('MenuType.id' => $types[$type]),
                    'fields' => 'MenuType.name'
                )
            );
            $conditions = $type ? array('MenuType.id' => $types[$type]) : null;
            $menus = $this->Menu->find('all',
                array(
                    'conditions' => $conditions,
                    'order' => 'MenuType.name ASC'
                )
            );
            foreach ($menus as $key => $menu) {
                $menus[$key]['Menu']['hasChild'] = 0;
                if ( $this->Menu->childcount($menu['Menu']['id']) ) {
                    $menus[$key]['Menu']['hasChild'] = 1;
                }
            }
            return array('lists' => $menus, 'name' => $menuName['MenuType']['name']);
        }
        
        return false;
    }
    
    /**
     * Mendapatkan semua menu
     * Ini diakses melalui method requestAction oleh
     * element menu.select.call_element.ctp
     * Tujuannya agar dapat dicaching selamanya
     * 
     * @author gedex
     * @access public
     * @return mixed mengembalikan array berupa menu jika diakses oleh element
     *               jika diakses melalui URL maka akan mengembalikan false
     */
    function menu_select() {
        // hindari direquest melalui URL
        if ( isset($this->params['requested']) ) {
            return $this->Menu->generateTreeList();
        }
        
        return false;
    }
    
    function menu_select_type() {
        // hindari direquest melalui URL
        if ( isset($this->params['requested']) ) {
            return $this->Menu->MenuType->find('list', array('fields' => array('id', 'name')));
        }
        
        return false;
    }
}
?>
